package org.linlinjava.litemall.wx.web;

import org.apache.ibatis.jdbc.Null;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallMoneyLog;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallBaseService;
import org.linlinjava.litemall.db.service.LitemallMoneyLogService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.beans.Transient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/moneyLog")
@Validated
public class WxMoneyLogController {

    @Autowired
    LitemallMoneyLogService litemallMoneyLogService;
    @Autowired
    private LitemallUserService litemallUserService;
    @Autowired
    private LitemallBaseService litemallBaseService;

    @GetMapping("info")
    public Object info(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "updatetime") String sort,
                       @RequestParam(defaultValue = "asc") String order,
                       @RequestParam String type
    ) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }

        /**
         * 这个是展示分页数据的moneyLogList
         */
        List<LitemallMoneyLog> moneyLogList = litemallMoneyLogService.findById(userId, page, limit, sort, order, type);

        double totalPay = 0;
        double totalIncome = 0;


        /**
         *这里是用来计算总收入和总支出的moneyLogList
         */

        List<LitemallMoneyLog> moneyLogs = litemallMoneyLogService.findAllById(userId, type);

        for (LitemallMoneyLog moneylog : moneyLogs) {

            if (moneylog.getIncome() != 0) {
                totalIncome += moneylog.getIncome();
            }
            if (moneylog.getPay() != 0) {
                totalPay += moneylog.getPay();
            }

        }

        Map<Object, Object> data = new HashMap<Object, Object>();
        data.put("totalIncome", totalIncome);
        data.put("totalPay", totalPay);
        data.put("moneylogList", moneyLogList);


        return ResponseUtil.ok(data);
    }

    /**
     * 申请 SUN  转奖励金
     *
     * @param userId
     * @param
     * @return
     */
    @PostMapping("/upmoney")
    public Object upjlj(@LoginUser Integer userId, @RequestParam String jianglijin) {

        double sun = Double.parseDouble(jianglijin);
        if (sun > 0.00) {
            LitemallUser user = litemallUserService.findById(userId);

            double mysun = user.getSUN();
            //        System.out.println(sun+"!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            if (mysun > sun) {

                double mysund = user.getSUND();
                double bl = Double.parseDouble(litemallBaseService.selectBYnumber("ZH003")) * 0.01;
                // double sxf = Double.parseDouble(litemallBaseService.selectBYnumber("ZH004")) * 0.01;
                double myjlj = user.getJianglijin();
                double setmyjlj = sun * mysund * bl;
                //  System.out.println(setmyjlj  +",");
                String setmyjljs = String.format("%.2f", setmyjlj).toString();
                setmyjlj = Double.parseDouble(setmyjljs);
                // System.out.println(setmyjlj  +"," +setmyjljs);
                litemallUserService.jljjj(userId, (myjlj + setmyjlj));
                litemallUserService.MoneyProdown(userId, sun, 2);

                litemallMoneyLogService.insertMoney(0, setmyjlj, "SUN转奖励金获得的奖励金", userId, "奖励金", 0, myjlj + setmyjlj, 0);
                litemallMoneyLogService.insertMoney(sun, 0, "SUN转奖励金失去的SUN", userId, "SUN", 0, mysun - sun, 0);


                return ResponseUtil.ok();

            } else {
                return ResponseUtil.fail(402, "余额不足");
            }


        } else {
            return ResponseUtil.fail(402, "请输入正确的 大于0的sun");
        }


    }
}
